﻿using HP48CSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestHP48CSharp48
{
    /// <summary>
    /// Test for HP48CSharp --Heap
    /// </summary>
    [TestClass]
    public class UnitTestHP48CSharp_Heap
    {

        /// <summary>
        /// Case 1.01 : Instance Heap is not null
        /// </summary>
        [TestMethod]
        public void TestHeap_IsNotNull()
        {
            Heap heap = new Heap();
            Assert.IsNotNull(heap, "The instance doesn't exist!");
        }

        /// <summary>
        /// Case 1.02 : Heap add differents type of elements in the same Heap
        /// </summary>
        [TestMethod]
        public void TestHeap_AddElements()
        {
            Heap heap = new Heap();
            InitialiseForTestHeap(heap);

            Assert.AreEqual(1, heap.Get(0));
            Assert.AreEqual("this is a string", heap.Get(1));
            Assert.AreEqual('c', heap.Get(2));
            Assert.AreEqual(3.141516, heap.Get(3));
        }

        /// <summary>
        /// Case 1.03 : Heap pop the last element
        /// </summary>
        [TestMethod]
        public void TestHeap_POP()
        {
            Heap heap = new Heap();
            InitialiseForTestHeap(heap);

            heap.Pop();

            Assert.AreEqual(3, heap.Count, "Pop doesn't work");
            Assert.AreEqual(1, heap.Get(0));
            Assert.AreEqual("this is a string", heap.Get(1));
            Assert.AreEqual('c', heap.Get(2));
        }

        /// <summary>
        /// Case 1.04 : Heap is a single collection of differents types of elements
        /// </summary>
        [TestMethod]
        public void TestHeap_IsCollection()
        {
            Heap heap = new Heap();
            InitialiseForTestHeap(heap);

            int count = 0;
            foreach (var item in heap)
            {
                switch (count)
                {
                    case 0:
                        Assert.AreEqual(1, heap.Get(0));
                        break;
                    case 1:
                        Assert.AreEqual("this is a string", heap.Get(1));
                        break;
                    case 2:
                        Assert.AreEqual('c', heap.Get(2));
                        break;
                    case 3:
                        Assert.AreEqual(3.141516, heap.Get(3));
                        break;
                    default:
                        break;
                }
                count++;
            }
        }

        /// <summary>
        /// Case 1.05 :  
        /// The two last elements int the heap can switch of position
        /// This operation is called "SWAP"
        /// </summary>
        [TestMethod]
        public void TestHeap_SwapElementInHeap()
        {
            Heap heap = new Heap();
            InitialiseForTestHeap(heap);

            heap.Swap();

            Assert.AreEqual(1, heap.Get(0));
            Assert.AreEqual("this is a string", heap.Get(1));
            Assert.AreEqual(3.141516, heap.Get(2));
            Assert.AreEqual('c', heap.Get(3));
        }


        /// <summary>
        /// Case 1.06 :  
        /// The user can enter operators (+, -, / *, etc)
        /// The system execute the operation with the last two elements entered
        /// and the result of the operation will be inserted in the heap
        /// </summary>
        [TestMethod]
        public void TestHeap_Operators()
        {
            Heap heap = new Heap();
            InitialiseForTestHeap(heap);
            heap.Add(5);
            heap.Add(6);
            //Add the '+' operator
            heap.Add('+');
            Assert.AreEqual(11, heap.Get(4), "Operator '+' doesn't work properly");

            heap.Add(20);
            heap.Add(60);
            //Add the '-' operator
            heap.Add('-');
            Assert.AreEqual(40, heap.Get(5), "Operator '-' doesn't work properly");

            heap.Add(3);
            heap.Add(4);
            //Add the '*' operator
            heap.Add('*');
            Assert.AreEqual(12, heap.Get(6), "Operator '*' doesn't work properly");

            heap.Add(2);
            heap.Add(50);
            //Add the '*' operator
            heap.Add('/');
            Assert.AreEqual(25, heap.Get(7), "Operator '/' doesn't work properly");
        }

        ///// <summary>
        ///// Case 1.07 :  
        ///// Reverse all the elements int the heap can switch of position
        ///// This operation is called "REVERSE"
        ///// </summary>
        //[TestMethod]
        //public void TestHeap_ReverseElementsInHeap()
        //{
        //    Heap heap = new Heap();
        //    InitialiseForTestHeap(heap);

        //    heap.Reverse();

        //    Assert.AreEqual(1, heap.Get(3));
        //    Assert.AreEqual("this is a string", heap.Get(2));
        //    Assert.AreEqual('c', heap.Get(1));
        //    Assert.AreEqual(3.141516, heap.Get(0));
        //}

        private static void InitialiseForTestHeap(Heap heap)
        {
            //Add element integer
            heap.Add(1);
            //Add element string
            heap.Add("this is a string");
            ////Add element character
            heap.Add('c');
            ////Add element double
            heap.Add(3.141516);
        }
    }
}
